#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jsmn.h"
#include "cJSON.h"
#include "esp_log.h"
#include "my_json_parse.h"
#include "my_dev_info.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

const static char *TAG = "my_json";

//解析节点配置json数据
void parse_note_data(const char *json)
{
    ESP_LOGI(TAG, "data :%s",json);
    cJSON *pJsonRoot = cJSON_Parse(json);
    note_json my_json;
    if( pJsonRoot != NULL){
        //解析authmode字段
        cJSON *pAppKey = cJSON_GetObjectItem(pJsonRoot, "app_key");
        if(pAppKey != NULL){
            if(cJSON_IsString(pAppKey))
                strcpy(my_json.app_key, pAppKey->valuestring); 
        }
    }

    printf("json appkey = %s\r\n", my_json.app_key);
    //将节点设置数据保存到flash;
    write_note_data(my_json);

    cJSON_Delete(pJsonRoot);
}


//解析json数据
void parse_ap_data(const char *json)
{   
    ESP_LOGI(TAG, "data :%s",json);
    cJSON *pJsonRoot = cJSON_Parse(json);

    ap_json my_json;

    if( pJsonRoot != NULL){
        //解析authmode字段
        cJSON *pAuthmode = cJSON_GetObjectItem(pJsonRoot, "authmode");
        if(pAuthmode != NULL){
            // printf("##########%s\n",cJSON_Print(pAuthmode));
            if(cJSON_IsString(pAuthmode))
                strcpy(my_json.authmode, pAuthmode->valuestring); 
        }
        // ESP_LOGI(TAG,"authmode = %s",my_json.authmode);

        //解析channel字段
        cJSON *pChannel = cJSON_GetObjectItem(pJsonRoot, "channel");
        if(pChannel != NULL){
            if(cJSON_IsString(pChannel)){
                my_json.channel = atoi(pChannel->valuestring);  //将字符转换为数字
            }
        }
        // ESP_LOGI(TAG,"channel = %d",my_json.channel);

        //解析ssid字段
        cJSON *pSsid = cJSON_GetObjectItem(pJsonRoot, "ssid");
        if(pSsid != NULL){
            if(cJSON_IsString(pSsid))
                strcpy(my_json.ssid, pSsid->valuestring);
        }
        // ESP_LOGI(TAG,"ssid = %s",my_json.ssid);

        //解析passwd字段
        cJSON *pPassword = cJSON_GetObjectItem(pJsonRoot, "password");
        if(pPassword != NULL){
            if(cJSON_IsString(pPassword))
                strcpy(my_json.passwd, pPassword->valuestring);
        }
        
        // ESP_LOGI(TAG,"passwd = %s",my_json.passwd);

    }

    //将web服务器配置数据保存到flash
    write_ap_data(my_json);

    // ap_json ap_json_t = {
    //     .channel = 1,
    //     .passwd = "123456789"
    // };
    // strcpy(ap_json_t.ssid, "aithinker01");
    // strcpy(ap_json_t.authmode, "WPAPSK/WPA2PSK");
    // ESP_LOGI(TAG,"authmode:%s, channel:%d, ssid=%s, passwd=%s", ap_json_t.authmode,ap_json_t.channel,ap_json_t.ssid,ap_json_t.passwd);
    // read_ap_data(&ap_json_t);
    // ESP_LOGI(TAG,"read authmode:%s, channel:%d, ssid=%s, passwd=%s", ap_json_t.authmode,ap_json_t.channel,ap_json_t.ssid,ap_json_t.passwd);
    cJSON_Delete(pJsonRoot);
    return;
}

//解析MQTT clinet配置参数
void parse_mqtt_client_data(const char *json)
{
    ESP_LOGI(TAG, "data :%s",json);
    cJSON *pJsonRoot = cJSON_Parse(json);

    // mqtt_client_parameter *mqtt_client_t = (mqtt_client_parameter *)malloc(1024);
    // if(mqtt_client_t == NULL){
    //     ESP_LOGE(TAG,"malloc failed\n");
    //     return;
    // }
    mqtt_client_parameter mqtt_client_t;
    if( pJsonRoot != NULL){
        //解析port字段
        cJSON *pPort = cJSON_GetObjectItem(pJsonRoot, "port");
        if(pPort != NULL){
            if(cJSON_IsString(pPort))
                 mqtt_client_t.port = atoi(pPort->valuestring);  //将字符转换为数字
        }
        // ESP_LOGI(TAG,"port = %d",mqtt_client_t.port);

        //解析addr字段
        cJSON *pAddr = cJSON_GetObjectItem(pJsonRoot, "addr");
        if(pAddr != NULL){
            if(cJSON_IsString(pAddr))
                strcpy(mqtt_client_t.host, pAddr->valuestring);
        }
        // ESP_LOGI(TAG,"host = %s",mqtt_client_t.host);

        //解析sub字段
        cJSON *pSub = cJSON_GetObjectItem(pJsonRoot, "sub");
        if(pSub != NULL){
            if(cJSON_IsString(pSub))
                strcpy(mqtt_client_t.subscribe_topic, pSub->valuestring);
        }
        // ESP_LOGI(TAG,"sub = %s",mqtt_client_t.subscribe_topic);

        //解析push字段
        cJSON *pPush = cJSON_GetObjectItem(pJsonRoot, "push");
        if(pPush != NULL){
            if(cJSON_IsString(pPush))
                strcpy(mqtt_client_t.public_topic, pPush->valuestring);
        }
        // ESP_LOGI(TAG,"push = %s",mqtt_client_t.public_topic);

        //解析user字段
        cJSON *pUser = cJSON_GetObjectItem(pJsonRoot, "user");
        if(pUser != NULL){
            if(cJSON_IsString(pUser))
                strcpy(mqtt_client_t.username, pUser->valuestring);
        }
        // ESP_LOGI(TAG,"username = %s",mqtt_client_t.username);

        //解析passwd字段
        cJSON *pPasswd = cJSON_GetObjectItem(pJsonRoot, "password");
        if(pPasswd != NULL){
            if(cJSON_IsString(pPasswd))
                strcpy(mqtt_client_t.passwd, pPasswd->valuestring);
        }
        // ESP_LOGI(TAG,"passwd = %s",mqtt_client_t.passwd);

        //解析qos字段
        cJSON *pQos = cJSON_GetObjectItem(pJsonRoot, "qos");
        if(pQos != NULL){
            if(cJSON_IsString(pQos))
                mqtt_client_t.qos = atoi(pQos->valuestring);  //将字符转换为数字
        }
        // ESP_LOGI(TAG,"qos = %d",mqtt_client_t.qos);

        //解析heart字段
        cJSON *pHeart = cJSON_GetObjectItem(pJsonRoot, "heartbeat");
        if(pQos != NULL){
            if(cJSON_IsString(pHeart))
                mqtt_client_t.heartbeat = atoi(pHeart->valuestring);  //将字符转换为数字
        }
        // ESP_LOGI(TAG,"heart = %d",mqtt_client_t.heartbeat);

        //解析autoconnect字段
        cJSON *pAuto = cJSON_GetObjectItem(pJsonRoot, "menuONOFF");
        if(pAuto != NULL){
            if(cJSON_IsString(pAuto))
                mqtt_client_t.autoconnect = atoi(pAuto->valuestring);  //将字符转换为数字
        }
        // ESP_LOGI(TAG,"autoconnect = %d",mqtt_client_t.autoconnect);

        //保存mqtt client参数到FLASH
        write_mqtt_client_data(mqtt_client_t);
        // free(mqtt_client_t);

        // mqtt_client_parameter mqtt_client_p;
        // read_mqtt_client_data(&mqtt_client_p);
        // printf("#####################\n");
        // my_printf(mqtt_client_p);   
    }
    cJSON_Delete(pJsonRoot);
    return;
}



//解析mqtt　server下行数据
mqtt_tranport_data parse_mqtt_user_data(const char *json)
{
    cJSON *pJsonRoot = cJSON_Parse(json);
    mqtt_tranport_data mqtt_tranport_data_t = {0};
    if( pJsonRoot != NULL){
        //解析opcode字段
        cJSON *pOpcode = cJSON_GetObjectItem(pJsonRoot, "opcode");
        if(pOpcode != NULL){
            if(cJSON_IsString(pOpcode))
                strcpy((char *)mqtt_tranport_data_t.opcode, pOpcode->valuestring);  
        }
        // ESP_LOGI(TAG,"opcode = %s",mqtt_tranport_data_t.opcode);

        //解析节点地址addr字段
        cJSON *pAddr = cJSON_GetObjectItem(pJsonRoot, "addr");
        if(pAddr != NULL){
            if(cJSON_IsString(pAddr))
                strcpy((char *)mqtt_tranport_data_t.addr, pAddr->valuestring);  
        }
        // ESP_LOGI(TAG,"addr = %s",mqtt_tranport_data_t.addr);

        //解析data字段
        cJSON *pData = cJSON_GetObjectItem(pJsonRoot, "data");
        if(pData != NULL){
            if(cJSON_IsString(pData))
                strcpy((char *)mqtt_tranport_data_t.data, pData->valuestring);  
        }
        // ESP_LOGI(TAG,"data = %s",mqtt_tranport_data_t.data);
    
    }
    cJSON_Delete(pJsonRoot);
    return mqtt_tranport_data_t;
}
